<template>
	<div class="branch-wrap">
		<div class="branch-box-wrap">
			<div class="branch-box">
				<el-button class="add-branch" type="success" plain round @click="addTerm">添加条件</el-button>
				<div class="col-box" v-for="(item,index) in nodeConfig.conditionNodes" :key="index">
					<div class="condition-node">
						<div class="condition-node-box">
							<div class="auto-judge" @click="(item.type !== 'default') && show(index)">
								<div class="sort-left" v-if="index!=0 && index!=nodeConfig.conditionNodes.length-1"
									@click.stop="arrTransfer(index,-1)">
									<el-icon><el-icon-arrow-left /></el-icon>
								</div>
								<div class="title">
									<span class="node-title">{{ item.name }}</span>
									<template v-if="item.type !== 'default'">
										<span class="priority-title">优先级{{index+1}}</span>
										<el-icon class="close" @click.stop="delTerm(index)"><el-icon-close /></el-icon>
									</template>
								</div>
								<div class="content">
									<em class="item-index" style="left: -44px; top:-18px">{{item.index}}</em>
									<template v-if="item.type !== 'default'">
										<span v-if="toText(nodeConfig, index)">{{ toText(nodeConfig, index) }}</span>
										<span v-else class="placeholder">请设置条件</span>
									</template>
									<template v-else>
										<span class="placeholder">其他条件进入此流程</span>
									</template>
								</div>
								<div class="sort-right" v-if="index<nodeConfig.conditionNodes.length-2"
									@click.stop="arrTransfer(index)">
									<el-icon><el-icon-arrow-right /></el-icon>
								</div>
							</div>
							<add-node v-model="item.childNode"></add-node>
						</div>
					</div>
					<slot v-if="item.childNode" :node="item"></slot>
					<div class="top-left-cover-line" v-if="index==0"></div>
					<div class="bottom-left-cover-line" v-if="index==0"></div>
					<div class="top-right-cover-line" v-if="index==nodeConfig.conditionNodes.length-1"></div>
					<div class="bottom-right-cover-line" v-if="index==nodeConfig.conditionNodes.length-1"></div>
				</div>
			</div>
			<add-node v-model="nodeConfig.childNode"></add-node>
		</div>
		<el-drawer title="条件设置" v-model="drawer" destroy-on-close append-to-body :size="980">
			<template #header>
				<div class="node-wrap-drawer__title">
					<label @click="editTitle" v-if="!isEditTitle">{{form.name}}<el-icon
							class="node-wrap-drawer__title-edit"><el-icon-edit /></el-icon></label>
					<el-input v-if="isEditTitle" ref="nodeTitle" v-model="form.name" clearable @blur="saveTitle"
						@keyup.enter="saveTitle"></el-input>
				</div>
			</template>
			<el-container>
				<el-main style="padding:0 20px 20px 20px">
					<el-form label-position="top">
						<el-form-item>
							<sc-form-table v-if="form.conditionNodes.length>0" v-model="form.conditionNodes"
								ref="conditionTable" :addTemplate="conditionTemplate" :drag-sort="false"
								placeholder="暂无数据" :border="false" hideAdd hideDelete>
								<el-table-column prop="name" label="分支描述" width="200">
									<template #default="scope">
										<el-input :disabled="scope.row.type=='default'" v-model="scope.row.name"
											placeholder="输入分支描述" />
									</template>
								</el-table-column>

								<el-table-column prop="value" label="条件表达式">
									<template #default="scope">
										<ta-cm-editor v-if="scope.row.type!=='default' && scope.row.properties"
											v-model="scope.row.properties.expression"></ta-cm-editor>
									</template>
								</el-table-column>

								<el-table-column label="中断" width="65" align="center">
									<template #default="scope">
										<el-switch v-if="scope.row.type!=='default' && scope.row.properties"
											v-model="scope.row.properties.isBreak" />
									</template>
								</el-table-column>

								<el-table-column width="55">
									<template #default="scope">
										<el-link v-if="scope.row.type!=='default'" type="danger" :underline="false"
											@click="delCase(scope.$index)">移除</el-link>
									</template>
								</el-table-column>
							</sc-form-table>
						</el-form-item>
						<p><el-button type="primary" icon="el-icon-plus" round @click="addCase">增加条件</el-button></p>
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="cancel">取消</el-button>
				</el-footer>
			</el-container>
		</el-drawer>
	</div>
</template>

<script>
	import addNode from './addNode'

	export default {
		props: {
			modelValue: {
				type: Object,
				default: () => {}
			}
		},
		components: {
			addNode
		},
		data() {
			return {
				nodeConfig: {},
				drawer: false,
				isEditTitle: false,
				index: 0,
				form: {},
				conditionList: [

				],
				conditionTemplate: {


				}
			}
		},
		watch: {
			modelValue() {
				this.nodeConfig = this.modelValue
			}
		},
		mounted() {
			this.nodeConfig = this.modelValue
		},
		methods: {
			str_random(length) {
				const base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
				let result = "";
				for (let i = 0; i < length; i++) {
					result += base58.charAt(Math.floor(Math.random() * base58.length));
				}
				return result;
			},
			safeStringify(obj, indent = 2) {
				let cache = [];
				const retVal = JSON.stringify(
					obj,
					(key, value) =>
					typeof value === "object" && value !== null ?
					cache.includes(value) ?
					undefined :
					cache.push(value) && value :
					value,
					indent
				);
				cache = null;
				return retVal;
			},
			show(index) {
				this.index = index
				this.form = {}
				this.form = JSON.parse(this.safeStringify(this.nodeConfig))

				console.error(this.form)

				this.drawer = true
			},
			editTitle() {
				this.isEditTitle = true
				this.$nextTick(() => {
					this.$refs.nodeTitle.focus()
				})
			},
			saveTitle() {
				this.isEditTitle = false
			},
			save() {
				// 如果还有一个节点就清空
				if (this.form.conditionNodes.length == 1) {
					if (this.nodeConfig.childNode) {
						if (this.nodeConfig.conditionNodes[0].childNode) {
							this.reData(this.nodeConfig.conditionNodes[0].childNode, this.nodeConfig.childNode)
						} else {
							this.nodeConfig.conditionNodes[0].childNode = this.nodeConfig.childNode
						}
					}
					this.$emit("update:modelValue", this.nodeConfig.conditionNodes[0].childNode);
				} else {
					this.nodeConfig = this.form
					this.$emit("update:modelValue", this.nodeConfig)
				}
				this.drawer = false
			},
			cancel() {
				// this.$emit("update:modelValue", this.form)
				this.drawer = false
			},
			addCase() {
				let insertPosition = this.form.conditionNodes.length - 1
				this.form.conditionNodes.splice(insertPosition, 0, {
					id: `${this.form.id}_case_${this.str_random(8)}`,
					name: "条件" + (insertPosition + 1),
					type: "case",
					properties: {
						isFirst: false,
						expression: "",
						isBreak: false,
						switchGroup: this.form.id
					}
				});
			},
			delCase(index) {
				this.form.conditionNodes.splice(index, 1);
			},
			addTerm() {
				let insertPosition = this.nodeConfig.conditionNodes.length - 1
				this.nodeConfig.conditionNodes.splice(insertPosition, 0, {
					id: `${this.nodeConfig.id}_case_${this.str_random(8)}`,
					name: "条件" + (insertPosition + 1),
					type: "case",
					properties: {
						isFirst: false,
						expression: "",
						isBreak: false,
						switchGroup: this.nodeConfig.id
					}
				});
			},
			delTerm(index) {
				this.nodeConfig.conditionNodes.splice(index, 1)
				if (this.nodeConfig.conditionNodes.length == 1) {
					if (this.nodeConfig.childNode) {
						if (this.nodeConfig.conditionNodes[0].childNode) {
							this.reData(this.nodeConfig.conditionNodes[0].childNode, this.nodeConfig.childNode)
						} else {
							this.nodeConfig.conditionNodes[0].childNode = this.nodeConfig.childNode
						}
					}
					this.$emit("update:modelValue", this.nodeConfig.conditionNodes[0].childNode);
				}
			},
			reData(data, addData) {
				if (!data.childNode) {
					data.childNode = addData
				} else {
					this.reData(data.childNode, addData)
				}
			},
			arrTransfer(index, type = 1) {
				this.nodeConfig.conditionNodes[index] = this.nodeConfig.conditionNodes.splice(index + type, 1, this
					.nodeConfig.conditionNodes[index])[0]
				this.$emit("update:modelValue", this.nodeConfig)
			},
			toText(nodeConfig, index) {
				var {
					properties
				} = nodeConfig.conditionNodes[index]
				if (properties?.expression) {
					const text = properties.expression
					return text
				} else {
					if (index == nodeConfig.conditionNodes.length - 1) {
						return "请设置条件"
					} else {
						return false
					}
				}
			}
		}
	}
</script>

<style>
</style>
